var Friend = {
    
   toAdd: function() {
                    SetLS("friendId",$("#id").text());
                    SetLS("friendph",$("#phone").text());
                    SetLS("friendnm",$("#name").text());
                    var person = ""+GetLS("searchInput");
                    var fid = GetLS("friendId");
                    var fph = ""+GetLS("friendph");
                    var fph1=fph.substring(fph.indexOf(":")+1);
                    var fnm = GetLS("friendnm");
                    var cph=CurrentUser.phone;
                    var adf=document.getElementById("btnCtn"); 
                    var chf=document.getElementById("btnCtnChat");  
                    if(CurrentUser.category==30){
                           if(person==cph ){
                               chf.setAttribute("style","display:none");
                              $alert("亲，不能添加自己为好友！");
                              //PCall("search_content","WinClose()");
                           }else{
                              var flag =false;
                              var length = CurrentUser.friends && CurrentUser.friends.length || 0;
                              for(var i=0; i<length; i++) {
                              var dbid=CurrentUser._id;
                              var fid = GetLS("friendId");
                              var dbfid=CurrentUser.friends[i].id;
                              var dbfr=CurrentUser.friends[i].from;
                              var st=CurrentUser.friends[i].status;
  
                                 var first = st==1;
                                 var second = (fid==dbfid || fid==dbfr);

                                 if((first&&second)==true){
                                     flag = true;
                                     //$alert("亲，你们已经是好友了!");
                                     //PCall("search_content","WinClose()");
                                     adf.setAttribute("style","display:none");  
                                     SetLS("alist_chat", true);
                                     SetLS("chat_detail", GetLS("search_detail"));
                                     OpenNewWin("chat_detail");
                                 }
                                 }  
                                 if(!flag){
                                    chf.setAttribute("style","display:none"); 
                                    OpenNewWin("add_friend");    
                                 }
                           }
                    }else{
                              if(person==cph ){
                                  chf.setAttribute("style","display:none");
                              return $alertEn("You can't add you as a friend!");
                              PCall("search_content","WinClose()");
                           }else{
                              var flag =false;
                              var length = CurrentUser.friends && CurrentUser.friends.length || 0;
                              for(var i=0; i<length; i++) {
                              var dbid=CurrentUser._id;
                              var fid = GetLS("friendId");
                              var dbfid=CurrentUser.friends[i].id;
                              var dbfr=CurrentUser.friends[i].from;
                              var st=CurrentUser.friends[i].status;
                                 var first = st==1;
                                 var second = (fid==dbfid || fid==dbfr);
                                 if((first&&second)==true){
                                     flag = true;
                                     //$alertEn("You are the friends alreay!");
                                     adf.setAttribute("style","display:none");
                                     SetLS("alist_chat", true);
                                     SetLS("chat_detail", GetLS("search_detail"));
                                     OpenNewWin("chat_detail");
                                     //PCall("search_content","WinClose()");
                                 }
                                 }  
                                 if(!flag){
                                    chf.setAttribute("style","display:none");
                                    OpenNewWin("add_friend");    
                                 }
                               
                               //OpenNewWin("add_friend");
                           }  
                   }
    },
    ChatQuery: function() {
                    SetLS("friendId",$("#id").text());
                    SetLS("friendph",$("#phone").text());
                    SetLS("friendnm",$("#name").text());
                    var person = ""+GetLS("searchInput");
                    var fid = GetLS("friendId");
                    var fph = ""+GetLS("friendph");
                    var fph1=fph.substring(fph.indexOf(":")+1);
                    var fnm = GetLS("friendnm");
                    var cph=CurrentUser.phone;
                    var adf=document.getElementById("btnCtn"); 
                    var chf=document.getElementById("btnCtnChat");  
                    if(CurrentUser.category==30){
                           if(person==cph ){
                               chf.setAttribute("style","display:none");
                              $alert("亲，不能添加自己为好友！");
                           }else{
                              var flag =false;
                              var length = CurrentUser.friends && CurrentUser.friends.length || 0;
                              for(var i=0; i<length; i++) {
                              var dbid=CurrentUser._id;
                              var fid = GetLS("friendId");
                              var dbfid=CurrentUser.friends[i].id;
                              var dbfr=CurrentUser.friends[i].from;
                              var st=CurrentUser.friends[i].status;
  
                                 var first = st==1;
                                 var second = (fid==dbfid || fid==dbfr);

                                 if((first&&second)==true){
                                     flag = true;
                                     adf.setAttribute("style","display:none");  
                                     SetLS("alist_chat", true);
                                     SetLS("chat_detail", GetLS("search_detail"));
                                 }
                                 }  
                                 if(!flag){
                                    chf.setAttribute("style","display:none"); 
                                 }
                           }
                    }else{
                              if(person==cph ){
                                  chf.setAttribute("style","display:none");
                              return $alertEn("You can't add you as a friend!");
                           }else{
                              var flag =false;
                              var length = CurrentUser.friends && CurrentUser.friends.length || 0;
                              for(var i=0; i<length; i++) {
                              var dbid=CurrentUser._id;
                              var fid = GetLS("friendId");
                              var dbfid=CurrentUser.friends[i].id;
                              var dbfr=CurrentUser.friends[i].from;
                              var st=CurrentUser.friends[i].status;
                                 var first = st==1;
                                 var second = (fid==dbfid || fid==dbfr);
                                 if((first&&second)==true){
                                     flag = true;
                                     adf.setAttribute("style","display:none");
                                     SetLS("alist_chat", true);
                                     SetLS("chat_detail", GetLS("search_detail"));
                                 }
                                 }  
                                 if(!flag){
                                    chf.setAttribute("style","display:none");
                                 }
                           }  
                   }
    },
    addFriend:function(){
      //条件： 1  当前用户ID 2  要添加的人的ID   3 输入的验证信息
      //  访问后台    ajax  接口 
      $ajax({
            url: "/message/sdm",
            data: {
               cid:CurrentUser._id,
               fid:GetLS("friendId"),
               cont:$("#userInput").val()
            },
            success: function(res) {
                //$alert("验证消息已发送，请耐心等待好友回复哦!");
                PCall("add_friend","WinClose()");
                PCall("search_detail","WinClose()");
                
            }
        });
    },
    initMessage:function(){
        $ajax({
            url: "/message/friendmessage",
            data:CurrentUser,
            success: function(res) {
             var items = res.result;
             SetLS("friendlist",items);
             users = GetLS("friendlist");
             console.log(items);
                var html = "";
                var length = items && items.length || 0;
                var de=CurrentUser.category==30?"删除":"Delete";
                for (var i=0; i<length; i++) {
                    html += '<div class="ub up_list" ontouchstart="zy_touch()" onclick=Friend.detail(' + i + ')>'+
                            '    <div class="uw3 uh3 ub_img uml_1f" style="background-image:url('+ Core.id2url(items[i].user) +')"></div>'+
                            '    <div class="ub-f1 ub ub-ver upl_p5">'+
                            '        <div class="ub-f1 ub" id="from" data-fr="'+items[i]._id+'" type="hidden">'+
                            '            <div class="ub-f1 ut-t ub_left" id="to" data-to="'+CurrentUser._id+'" type="hidden">'+ items[i].user.name +' ' + '<span class="col_r">'+(CurrentUser.category==30?"请求加你为好友:":"Request to be your friend:")+'</span>'+'</div>'+
                            '        </div>'+
                            '        <div class="ub_left ub-f1 uh2 umt_p5">'+ (items[i].content?items[i].content:CurrentUser.category==30?"这个家伙很懒，什么也没留下！":"No Message") +
                            '    </div>'+
                            '</div>'+
                            '</div>'+
                            '    <div id="btnCtn" class="ub_right">'+
                            '      <div id="deldata" data-fr="'+items[i].user._id+'" data-to="'+CurrentUser._id+'" data-ontouchstart="zy_touch()" onclick="Chat.remove();" class="btn uw3 umr_p2 ub_right ufp6 bc_base t-wh umt_10fm">'+de+'</div>'+
                            '    </div>';
                }
                $("#ctn").prepend(html);
               }
            });
    },
    detail:function(i) {
      var users = GetLS("friendlist");
      var item =  users[i].user;
      console.log(item);
      SetLS("friend_detail", item);
      OpenNewWin("friend_detail");
    },
    detailInit: function() {
        var friend = GetLS("friend_detail");
        $("#id").text(friend._id);
        $("#photo").css("background-image", "url("+ Core.id2url(friend) +")");
        $("#name").text(friend.name +"　"+ (friend.sex?friend.sex:""));
        $("#phone").text(CurrentUser.category==30?"电话:"+friend.phone:"Phone:"+friend.phone);
        var wu=CurrentUser.category==30?"无":"No";
        $("#country").text(CurrentUser.category==30?"国家:"+ (friend.courntry?friend.courntry:wu):"Country:"+ (friend.courntry?friend.courntry:wu));
        $("#interest").text(CurrentUser.category==30?"爱好:"+ (friend.interest?friend.interest:wu):"Interest:"+ (friend.interest?friend.interest:wu));
    },
    reply:function(index){
       $ajax({
           url:"/user/rep",
           data:{
               user:CurrentUser,
               fid:$("#id").text(),
               yz:index
           },
           success:function(res){
               SetLS("user",res.result);
               var ml=CurrentUser.category==30?"../ch/":"../en/";
               if(CurrentUser.category==30){
                   if(index==1){
                  //return $alert("已同意，可以去通讯录和好友聊天了"); 
                  PCall("friend_detail","WinClose()");
                  PCall(ml+"chat","WinClose()");
                  OpenNewWin("address");
               }else{
                  PCall(ml+"chat","WinClose()");
                  WinClose();
                  OpenNewWin("address");
               }
               }else{
                  if(index==1){
                  //return $alert("已同意，可以去通讯录和好友聊天了"); 
                  PCall("friend_detail","WinClose()");
                  PCall(ml+"chat","WinClose()");
                  OpenNewWin("address");
               }else{
                  PCall(ml+"chat","WinClose()");
                  WinClose();
                  OpenNewWin("address");
               } 
               }
           }
           }); 
    },
}